jQ(function(){
	jQ("div.my_clips_menu a.clipmanage").addClass("clipmanageonit");
	var eventid=eval(getParameter("eventid"));
	if(isNaN(eventid)||eventid==null){
		window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html";
	}else{
		getClipDetail(eventid);
	}
	jQ("#left_clip_list").empty();
	jQ("#mainpart_list_content").data("currentpage",0);
	jQ("#clipmanagehead li").click(function(){
		var qb=jQ(this).attr("queryby");
		if(qb=="cache"||qb=="recycle"){
			window.location.href=jQ("#ctxVal").val()+"/myclip/clip_"+qb+".html";
		}else{
			window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html#"+qb;
		}
	});
	loadCacheAndClockAndRecycleCount();
	jQ("#viewopt").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		closeOnEscape:false,
		draggable:false,
		height:200,
		buttons: {
			
			"确定": function() {
				var opt=jQ("#viewopt");
				var triggerfun=opt.data("function");
				opt.trigger(triggerfun);
				opt.removeData("function");
				jQ("#viewopt_p").hide();
				jQ("#viewopt_img").show();
				activeBtnOnDialog("viewopt",false);
				//jQ(this).dialog('close');
			},
			"取消": function() {
				var viewopt=jQ("#viewopt");
				var triggerfun=jQ("#viewopt").data("function");
				viewopt.unbind(triggerfun);
				viewopt.find("input.eventid").val("");
				viewopt.removeData("function");
				jQ(this).dialog('close');
			}
		},
		close: function() {
			jQ("#viewopt_p").show();
			jQ("#viewopt_img").hide();
			activeBtnOnDialog("viewopt",true);
		},
		open:function(){
		}
	});
});

function loadCacheAndClockAndRecycleCount(){
	var eventid=jQ("#viewopt_eventid").val(); 
	jQ.ajax({
		type: "POST",
		url:"manageClipAjax!loadCacheAndClockAndRecycleCount.action",
		success:function(data){
			if(data!=null){
				var dataObj=eval("("+data+")");//转换为json对象
				if(dataObj['result']){
					
					var alerttotalcount=dataObj.alerttotalcount;
					var memototalcount=dataObj.memototalcount;
					var invitetotalcount=dataObj.invitetotalcount;
					var cachetotalcount=dataObj.cachetotalcount;
					var recycletotalcount=dataObj.recycletotalcount;
					
					
					alerttotalcount=alerttotalcount>99?"99+":alerttotalcount;
					memototalcount=memototalcount>99?"99+":memototalcount;
					invitetotalcount=invitetotalcount>99?"99+":invitetotalcount;
					cachetotalcount=cachetotalcount>99?"99+":cachetotalcount;
					recycletotalcount=recycletotalcount>99?"99+":recycletotalcount;
					
					jQ("#memocount>span.count").empty().append(memototalcount);
					jQ("#invitecount>span.count").empty().append(invitetotalcount);
					jQ("#alertcount>span.count").empty().append(alerttotalcount);
					jQ("#cachecount>span.count").empty().append(cachetotalcount);
					jQ("#recyclecount>span.count").empty().append(recycletotalcount);
					
				}
			}
		}
	});
}

function getClipDetail(eventid){
	jQ.ajax({
		type:"POST",
		url: "manageClipAjax!loadRecycleclip.action",
		data:{
			eventid:eval(eventid)
		},
		success:function(data){
			if(!data.result){
				//加入无效页面
				jQ("#clip_detail_main").hide();
				jQ("#nonecliptip").show();
				return;
			}
			jQ("#clip_detail_main").show();
			var eventobj=data.clipevent;
			var eventid=eval(eventobj.eventid);
			jQ("#clipevent-eventid").val(eventid);
			//loadClipMessage(eventid);
			var title=eventobj.title;
			var starttime=eval(eventobj.starttime);
			var endtime=eval(eventobj.endtime);
			var iswholeday=eval(eventobj.iswholeday);
			var content=eventobj.content;
			var clocktime=eval(eventobj.clocktime);
			var processstatus=eval(eventobj.processstatus);
			var clipstatus=eval(eventobj.clipstatus);
			var reliedclip=eval(eventobj.reliedclip);
			
			var uname=eventobj.uname;
			var unameid=eventobj.unameid;
			var unamelogo=eventobj.unamelogo;
			var unameslogo=eventobj.unameslogo;
			
			var cliptype=eval(eventobj.cliptype);
			var seid=eventobj.seid;
			var isuserself=eventobj.isuserself;
			var shareid=eventobj.shareid;
			var eventtype=eventobj.eventtype;
			var clipeventpic=eventobj.clipeventpic;
			var clipiscc=eventobj.clipiscc;
			initDetailTimeCalendar(starttime,iswholeday,endtime);
			
			jQ("#clipevent-eventid").data("shareid",shareid);
			jQ("#clipevent-eventid").data("starttime",starttime);
			jQ("#clipevent-eventid").data("title",title);
			jQ("#clipevent-eventid").data("seid",seid);
			jQ("#clipevent-eventid").data("processstatus",processstatus);
			jQ("#clipevent-eventid").data("reliedclip",reliedclip);
			jQ("#clipevent-eventid").data("eventtype",eventtype);
			jQ("#clipevent-eventid").data("systemmsgid",data.systemmsgid);
			jQ("#clipevent-eventid").val(eventid);
			//统计
			jQ("#event-unamelogo").attr("title",uname);
			jQ("#event-unamelogo").css({"background":"url('"+unamelogo+"')"});
			jQ("#event-unamelogo").bind("click",function(){
				window.location.href=jQ("#ctxVal").val()+"/myclip/user.html?"+unameid;
			});
			jQ("#event-uname").empty().append(uname);
			if(clipiscc)
				jQ("#event-uname").next().show();
			else
				jQ("#event-uname").next().hide();
			if(cliptype==3){
				if(seid!=0&&seid!=reliedclip)
					jQ("#event-cliptype").attr("class","invite").show();
				else
					jQ("#event-cliptype").hide();
			}else if(cliptype==4){
				jQ("#event-cliptype").attr("class","request").show();
			}else{
				jQ("#event-cliptype").hide();
			}
			if(eventtype==2&&seid>0){
				if(reliedclip!=seid){
					var sourceeventid=eventobj.sourceeventid;
					var sourceuid=eventobj.sourceuid;
					var sourceuname=eventobj.sourceuname;
					var sourceulogo=eventobj.sourceulogo;
					jQ("#event-sourcelogo").css({"background":"url('"+sourceulogo+"')"});
					jQ("#event-sourcelogo").attr("title",sourceuname);
					jQ("#event-sourcelogo").bind("click",function(){
						window.location.href=jQ("#ctxVal").val()+"/myclip/user.html?"+sourceuid;
					});
					jQ("#event-sourcelogo").parent().children().show();
				}
			}
			
			if(iswholeday)
				jQ("#event-time").empty().append("<span style='width:auto;padding-top:0px;'>开始于</span><b>"+formateTitleTime(starttime)+"</b>");
			else
				jQ("#event-time").empty().append("<b>"+formateTitleTime(starttime)+"</b><b>至</b><b>"+formateTitleTime(endtime)+"</b>");
			/*if(processstatus==-1){
				jQ("#event-processtatus").removeClass().addClass("edt_before").empty().append("未开始");
			}else if(processstatus==0){
				jQ("#event-processtatus").removeClass().addClass("edt_ing").empty().append("进行中");
			}else if(processstatus==1){
				jQ("#event-processtatus").removeClass().addClass("edt_over").empty().append("已结束");
			}else if(processstatus==2){
				jQ("#event-processtatus").removeClass().addClass("edt_stop").empty().append("已停止");
			}*/
			
			jQ("#event-title").empty().append(title);
			//详情
			if(content.length>0||clipeventpic.length>0){
				if(content.length>0)
					jQ("#event-detail").empty().append(content);
				if(clipeventpic.length>0)
					jQ("#event-detail").append("<img src='"+clipeventpic+"'/>");
				jQ("#event-detail").parent().show();
			}else{
				jQ("#event-detail").parent().hide();
			}
			initDetailOptBtn();
		}
	});
}

function initDetailTimeCalendar(starttime,iswholeday,endtime){
	var startdate=new Date();
	startdate.setTime(starttime);
	var year=startdate.getFullYear();
	var month=startdate.getMonth();
	startdate=new Date(year,month,startdate.getDate());
	var enddate;
	var issameday=false;
	if(!iswholeday){
		enddate=new Date();
		enddate.setTime(endtime);
		enddate=new Date(enddate.getFullYear(),enddate.getMonth(),enddate.getDate());
		issameday=startdate.getTime()==enddate.getTime();
	}else{
		issameday=true;
	}
	detailtimemonth(null,startdate,enddate,issameday,year,month);
}

function detailtimemonth(event,startdate,enddate,issameday,year,month){
	if(typeof(startdate)=='undefined'){
		startdate=event.data.startdate;
		issameday=event.data.issameday;
		enddate=event.data.enddate;
		month=event.data.month;
		year=event.data.year;
	}
	var firstDateOfMonth=new Date(year,month,1);
	var title=firstDateOfMonth.pattern("yyyy年MM月");
	jQ("#calendarzone div.title span.title").empty().append(title);
	var week2=firstDateOfMonth.getDay();
	var week1=0;
	var startfromdate=new Date(year,month,1);
	if(week2>week1){
		startfromdate.setDate(firstDateOfMonth.getDate() -week2);
	}
	var endyear,endmonth;
	if(month==11){
		endmonth=0;
		endyear=year+1;
	}else{
		endmonth=month+1;
		endyear=year;
	}
	
	var endDateOfMonth=new Date(endyear,endmonth,1);
	endDateOfMonth.setDate(endDateOfMonth.getDate() -1);
	var week6=6;
	var week5=endDateOfMonth.getDay();
	
	
	var endtodate=new Date(endyear,endmonth,1);
	endtodate.setDate(endtodate.getDate() -1);
	if(week5<week6){
		endtodate.setDate(endDateOfMonth.getDate()+ (week6-week5));
	}
	var index=0;
	
	while(startfromdate.getTime()<=endtodate.getTime()){
		var tdobj=jQ("#tddetail"+index);
		tdobj.empty().append(startfromdate.getDate());
		tdobj.removeClass();
		if(startfromdate.getMonth()!=firstDateOfMonth.getMonth(0)){
			if(!issameday){
				if(startfromdate.getTime()>=startdate.getTime()&&startfromdate.getTime()<enddate.getTime()+1){
					tdobj.addClass("selected");
				}else{
					tdobj.addClass("other");
				}
			}
		}else{
			if(issameday){
				if(startfromdate.getTime()==startdate.getTime()){
					tdobj.addClass("selected");
				}
			}else{
				if(startfromdate.getTime()>=startdate.getTime()&&startfromdate.getTime()<enddate.getTime()+1){
					tdobj.addClass("selected");
				}
			}
		}
		index++;
		startfromdate.setDate(startfromdate.getDate() +1);
	}
	if(index==28){
		jQ("#detailsharetimetable tr[line=4]").hide();
	}else if(index==35){
		jQ("#detailsharetimetable tr[line=4]").show();
		jQ("#detailsharetimetable tr[line=5]").hide();
	}else{
		jQ("#detailsharetimetable tr[line=4]").show();
		jQ("#detailsharetimetable tr[line=5]").show();
	}
	
	if(issameday){
		jQ("#calendarzone div.title a").addClass("none");
	}else{
		if(startdate.getTime()<firstDateOfMonth.getTime()){
			var temp1=new Date(firstDateOfMonth.getFullYear(),firstDateOfMonth.getMonth(),firstDateOfMonth.getDate());
			temp1.setMonth(temp1.getMonth()-1);
			var tempyear=temp1.getFullYear();
			var tempmonth=temp1.getMonth();
			jQ("#calendarzone div.title a.prev").removeClass("none").unbind("click")
				.bind("click",{startdate:startdate,enddate:enddate,issameday:issameday,year:tempyear,month:tempmonth},detailtimemonth);
		}else{
			jQ("#calendarzone div.title a.prev").addClass("none").unbind("click");
		}
		var tempdate=new Date(endDateOfMonth.getFullYear(),endDateOfMonth.getMonth(),endDateOfMonth.getDate());
		tempdate.setDate(tempdate.getDate()+ 1);
		if(enddate.getTime()>tempdate.getTime()){
			var tempyear=tempdate.getFullYear();
			var tempmonth=tempdate.getMonth();
			jQ("#calendarzone div.title a.next").removeClass("none").unbind("click")
			.bind("click",{startdate:startdate,enddate:enddate,issameday:issameday,year:tempyear,month:tempmonth},detailtimemonth);
		}else{
			jQ("#calendarzone div.title a.next").addClass("none").unbind("click");
		}
	}
}

function loadClipMessage(eventid){
	jQ.ajax({
		type: "POST",
		url:"eventAjax!loadClipMessage.action",
		data:{
			eventid:eval(eventid)
		},
		success:function(data){
			if(data!=null){
				if(jQ(data).find("Clipmessage").length==0){
					return;
				}
				var html="";
				jQ(data).find("Clipmessage").each(function(){
					var content=jQ("content",this).text();
					var email=jQ(this).find("email").text();
					var replytime=eval(jQ(this).find("replytime").text());
					var name=jQ(this).find("name").text();
					var uid=jQ(this).find("uid").text();
					var logo=jQ(this).find("logo").text();
					var replystr=formateDetailTime(replytime);
					var tmp="<li class='leave_msg'>";
					tmp+="<dt>";
					tmp+="<a class='logo' style='background:url(\""+logo+"\")' href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'></a>";
					tmp+="<span class='fid'>";
					tmp+="<a href='"+jQ("#ctxVal").val()+"/myclip/user.html?"+uid+"'>"+name+"</a>";
					tmp+="</span>";
					tmp+="<div>";
					tmp+=htmlData(content);
					tmp+="</div>";
					tmp+="<span class='time'>";
					tmp+=replystr;
					tmp+="</span></dt>";
					tmp+="</li>";
					html+=tmp;
				});
				jQ("#clipevent-messages").empty().append(html);
			}
		}
	});
}

function initDetailOptBtn(){
	var del=jQ("#clipevent-opt a.ignore");
	if(del.css("display")!='none'){
		del.unbind("click").bind("click",deleteDetailEvent);
	}
}

function deleteDetailEvent(event){
	var systemmsgid=eval(jQ("#clipevent-eventid").data("systemmsgid"));
	jQ.ajax({
		type : "POST",
		url : "messageAjax!deleteSystemMessage.action",
		data : {
			systemmsgid:systemmsgid
		},
		success : function(data){
			if(data.result){
				window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html";
			}else{
				alert(data.errormsg);
			}
		}
	});
}



function initDetailTimeSelect(){
	jQ("tr.clipevent-set_clock td select.time_hour").empty();
	var html="";
	for(var i=0;i<24;i++){
		if(i==0)
			html+="<option value="+i+">0"+i+"</option>";
		else if(i<10)
			html+="<option value="+i+">0"+i+"</option>";
		else
			html+="<option value="+i+">"+i+"</option>";
	}
	jQ("tr.clipevent-set_clock td select.time_hour").append(html);
	html="";
	jQ("tr.clipevent-set_clock td select.time_min").empty();
	for(var i=0;i<12;i++){
		var j=i*5;
		if(j==0)
			html+="<option value="+j+">0"+j+"</option>";
		else if(j<10)
			html+="<option value="+j+">0"+j+"</option>";
		else
			html+="<option value="+j+">"+j+"</option>";
	}
	jQ("tr.clipevent-set_clock td select.time_min").append(html);
}

function backPage(){
	var currentpage=eval(getParameter("currentpage"));
	if(isNaN(currentpage)||currentpage==null){
		window.location.href=jQ("#ctxVal").val()+"/myclip/clip_recycle.html";
	}else{
		window.location.href=jQ("#ctxVal").val()+"/myclip/clip_recycle.html?currentpage="+currentpage;
	}
}

function formateDetailTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy-MM-dd HH:mm");
}

function formateTitleTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy-MM-dd");
}

function getTypeName(eng){
	if(eng=='other'){
		return "其他";
	}else if(eng=='work'){
		return "工作";
	}else if(eng=='socal'){
		return "社交";
	}else if(eng=='sect'){
		return "私人";
	}
}

function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=decodeURIComponent(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.indexOf("#")>0){
    	paramStr=paramStr.substring(0,paramStr.indexOf("#"));
    }
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            p = p[1];
        }
    }
    return p;
}